package com.example.demo.zerocopy;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

import static java.nio.channels.FileChannel.MapMode.READ_WRITE;

public class ZeroCopyTest {
    public static void main(String[] args) throws IOException {


        try (FileChannel inChannel = new FileInputStream("D:\\test.txt").getChannel();
             FileChannel outChannel = new FileOutputStream("D:\\test2.txt").getChannel()) {
            //sendfile
            //下面两个方法效果一致
            inChannel.transferTo(0, inChannel.size(), outChannel);
            outChannel.transferFrom(inChannel, 0, inChannel.size());

            //mmap
            MappedByteBuffer load = inChannel.map(READ_WRITE, 0, inChannel.size()).load();
            int write = outChannel.write(load);
        } catch (Exception e) {
            System.out.println();
        }
    }
}
